package com.juyu.miliao.api.controller;

import com.fasterxml.jackson.annotation.JsonView;
import com.juyu.miliao.api.domain.Follow;
import com.juyu.miliao.api.domain.User;
import com.juyu.miliao.api.service.FollowService;
import com.juyu.miliao.api.util.JacksonView;
import com.juyu.miliao.api.validator.FollowCheck;
import com.juyu.miliao.common.domain.Subject;
import com.juyu.miliao.common.exception.IllegalParamException;
import net.sf.json.JSONObject;
import org.springframework.security.core.Authentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 关注列表
 * 
 * @author wbf
 */
@RestController
@RequestMapping("/follow")
public class FollowContorller {

    @Resource
	FollowService followService;
	/**
	 * 当前用户是否关注过某userId
	 */
	@GetMapping("/{userId}")
	public JSONObject getFollowRelation(Authentication token,@PathVariable("userId") Long userId) {
		Subject subject = (Subject) token.getPrincipal();
		long tokenUserId = subject.getId();
		JSONObject json = new JSONObject();
		String temp = followService.getFollowRelation(userId,tokenUserId);
		json.put("isFollow", temp);
		return json;
	}

	/**
	 * 当前用户是否被某userId关注过
	 */
	@GetMapping("/passive/{userId}")
	public JSONObject getFollowRelationPassive(Authentication token,@PathVariable("userId") Long userId) {
		Subject subject = (Subject) token.getPrincipal();
		long tokenUserId = subject.getId();
		JSONObject json = new JSONObject();
		String temp = followService.getFollowRelationPassive(userId,tokenUserId);
		json.put("isPassiveFollow", temp);
		return json;
	}


	/**
	 * 获取关注列表
	 */
	@GetMapping("/list/{page}-{pageSize}")
	@JsonView(JacksonView.Follow.class)
	public List<User> getFollowById(Authentication token, @PathVariable("page") Integer page,
			@PathVariable("pageSize") Integer pageSize) {
		Subject subject = (Subject) token.getPrincipal();
		long userId = subject.getId();
		if (page <= 0) {
			page = 1;
		}
		if (pageSize <= 0) {
			pageSize = 20;
		}
		return followService.getFollowById(userId, page, pageSize);
	}

	/**
	 * 获取关注列表miliao1.2
	 */
	@GetMapping("/list")
	@JsonView(JacksonView.Follow.class)
	public List<User> getFollowById(Authentication token) {
		Subject subject = (Subject) token.getPrincipal();
		long userId = subject.getId();
		return followService.getFollowListById(userId);
	}
	
	/**
	 * 获取粉丝列表
	 */
	@GetMapping("/fans/{page}-{pageSize}")
	@JsonView(JacksonView.Follow.class)
	public List<User> getFansById(Authentication token, @PathVariable("page") Integer page,
			@PathVariable("pageSize") Integer pageSize) {
		Subject subject = (Subject) token.getPrincipal();
		long userId = subject.getId();
		if (page <= 0) {
			page = 1;
		}
		if (pageSize <= 0) {
			pageSize = 20;
		}
		return followService.getFansById(userId, page, pageSize);
	}
	
	/**
	 * 新增关注
	 */
	@PostMapping()
	public void insertFollow(Authentication token, @Validated(FollowCheck.class) Follow entity) {
		Subject subject = (Subject) token.getPrincipal();
		if (subject.getId() != entity.getUserId())
			throw new IllegalParamException("用户id错误");
		followService.insertFollow(entity);
	}

	/**
	 * 取消关注
	 */
	@DeleteMapping("/{userId:^[1-9][0-9]*$}/{followUserId:^[1-9][0-9]*$}")
	public void deleteFollow(Authentication token, @PathVariable("userId") Integer userId,
			@PathVariable("followUserId") Integer followUserId) {
		Subject subject = (Subject) token.getPrincipal();
		if (subject.getId() != userId)
			throw new IllegalParamException("用户id错误");
		Follow entity = new Follow(userId,followUserId);
		followService.deleteFollow(entity);
	}


}
